<template>
  <div class="p-2">
    <!-- 消息存取 - 历史数据管理 -->
    <!-- 查询表单 -->
    <el-card shadow="hover" style="margin-bottom: 8px">
      <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="100px">
        <el-form-item label="设备ID" prop="deviceId">
          <el-input v-model="queryParams.deviceId" placeholder="请输入设备ID" clearable style="width: 200px" />
        </el-form-item>
        <el-form-item label="数据类型" prop="dataType">
          <el-select v-model="queryParams.dataType" placeholder="请选择数据类型" clearable style="width: 150px">
            <el-option label="全部" value="" />
            <el-option label="传感器数据" value="sensor" />
            <el-option label="状态数据" value="status" />
            <el-option label="告警数据" value="alarm" />
            <el-option label="控制指令" value="control" />
            <el-option label="系统日志" value="log" />
          </el-select>
        </el-form-item>
        <el-form-item label="主题" prop="topic">
          <el-input v-model="queryParams.topic" placeholder="请输入主题" clearable style="width: 200px" />
        </el-form-item>
        <el-form-item label="时间范围" prop="timeRange">
          <el-date-picker
            v-model="queryParams.timeRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 350px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 统计卡片 -->
    <el-row :gutter="20" style="margin-bottom: 8px">
      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-item">
            <div class="stat-icon" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%)">
              <el-icon size="24"><DataBoard /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ statistics.totalRecords }}</div>
              <div class="stat-label">总记录数</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-item">
            <div class="stat-icon" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%)">
              <el-icon size="24"><Clock /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ statistics.todayRecords }}</div>
              <div class="stat-label">今日记录</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-item">
            <div class="stat-icon" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)">
              <el-icon size="24"><Cpu /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ statistics.activeDevices }}</div>
              <div class="stat-label">活跃设备</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-item">
            <div class="stat-icon" style="background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)">
              <el-icon size="24"><Folder /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ statistics.storageSize }}</div>
              <div class="stat-label">存储大小</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 操作按钮 -->
    <el-card shadow="hover" style="margin-bottom: 8px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Download" @click="handleExport">导出数据</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="Refresh" @click="handleRefresh">刷新数据</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="PieChart" @click="showStatistics">统计分析</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="info" plain icon="Setting" @click="showSettings">存储设置</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="Delete" @click="handleCleanup">数据清理</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Monitor" @click="showRealtime">实时监控</el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 数据表格 -->
    <el-card shadow="hover">
      <el-table v-loading="loading" :data="dataList" border stripe>
        <el-table-column label="序号" type="index" width="60" align="center" />
        <el-table-column label="设备ID" prop="deviceId" width="150" show-overflow-tooltip>
          <template #default="scope">
            <el-link type="primary" @click="viewDeviceDetail(scope.row)">{{ scope.row.deviceId }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="设备名称" prop="deviceName" width="150" show-overflow-tooltip />
        <el-table-column label="数据类型" prop="dataType" width="120" align="center">
          <template #default="scope">
            <el-tag :type="getDataTypeTag(scope.row.dataType)" size="small">{{ getDataTypeText(scope.row.dataType) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="主题" prop="topic" width="200" show-overflow-tooltip />
        <el-table-column label="数据内容" prop="payload" show-overflow-tooltip>
          <template #default="scope">
            <div class="payload-content" @click="viewPayload(scope.row)">
              {{ formatPayload(scope.row.payload) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="QoS" prop="qos" width="80" align="center">
          <template #default="scope">
            <el-tag :type="getQosTag(scope.row.qos)" size="small">{{ scope.row.qos }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="数据大小" prop="dataSize" width="100" align="center">
          <template #default="scope">
            <span>{{ formatDataSize(scope.row.dataSize) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="接收时间" prop="receiveTime" width="160" show-overflow-tooltip />
        <el-table-column label="存储状态" prop="storageStatus" width="100" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.storageStatus === 'stored' ? 'success' : 'warning'" size="small">
              {{ scope.row.storageStatus === 'stored' ? '已存储' : '缓存中' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="240" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="查看详情" placement="top">
              <el-button link type="primary" icon="View" @click="viewDetail(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="查看原始数据" placement="top">
              <el-button link type="success" icon="Document" @click="viewRawData(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="数据分析" placement="top">
              <el-button link type="warning" icon="TrendCharts" @click="analyzeData(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button link type="info" icon="Download" @click="exportSingle(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="danger" icon="Delete" @click="deleteRecord(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 数据详情对话框 -->
    <el-dialog title="数据详情" v-model="detailDialog.visible" width="800px" append-to-body>
      <div v-if="detailDialog.data">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="设备ID">{{ detailDialog.data.deviceId }}</el-descriptions-item>
          <el-descriptions-item label="设备名称">{{ detailDialog.data.deviceName }}</el-descriptions-item>
          <el-descriptions-item label="数据类型">
            <el-tag :type="getDataTypeTag(detailDialog.data.dataType)" size="small">{{ getDataTypeText(detailDialog.data.dataType) }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="主题">{{ detailDialog.data.topic }}</el-descriptions-item>
          <el-descriptions-item label="QoS等级">
            <el-tag :type="getQosTag(detailDialog.data.qos)" size="small">{{ detailDialog.data.qos }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="数据大小">{{ formatDataSize(detailDialog.data.dataSize) }}</el-descriptions-item>
          <el-descriptions-item label="接收时间">{{ detailDialog.data.receiveTime }}</el-descriptions-item>
          <el-descriptions-item label="存储状态">
            <el-tag :type="detailDialog.data.storageStatus === 'stored' ? 'success' : 'warning'" size="small">
              {{ detailDialog.data.storageStatus === 'stored' ? '已存储' : '缓存中' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="数据内容" :span="2">
            <div class="payload-detail">
              <pre>{{ JSON.stringify(JSON.parse(detailDialog.data.payload || '{}'), null, 2) }}</pre>
            </div>
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-dialog>

    <!-- 原始数据对话框 -->
    <el-dialog title="原始数据" v-model="rawDataDialog.visible" width="900px" append-to-body>
      <div class="raw-data-container">
        <el-tabs v-model="rawDataTab">
          <el-tab-pane label="格式化JSON" name="formatted">
            <div class="code-editor">
              <pre>{{ formattedRawData }}</pre>
            </div>
          </el-tab-pane>
          <el-tab-pane label="原始文本" name="raw">
            <div class="code-editor">
              <pre>{{ rawDataDialog.data }}</pre>
            </div>
          </el-tab-pane>
          <el-tab-pane label="十六进制" name="hex">
            <div class="code-editor">
              <pre>{{ hexRawData }}</pre>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="copyRawData">复制数据</el-button>
          <el-button type="primary" @click="rawDataDialog.visible = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 统计分析对话框 -->
    <el-dialog title="统计分析" v-model="statisticsDialog.visible" width="1200px" append-to-body>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-card shadow="hover">
            <template #header>
              <span>数据类型分布</span>
            </template>
            <div class="chart-container" ref="dataTypeChart"></div>
          </el-card>
        </el-col>
        <el-col :span="12">
          <el-card shadow="hover">
            <template #header>
              <span>设备活跃度</span>
            </template>
            <div class="chart-container" ref="deviceActivityChart"></div>
          </el-card>
        </el-col>
      </el-row>
      <el-row :gutter="20" style="margin-top: 20px">
        <el-col :span="24">
          <el-card shadow="hover">
            <template #header>
              <span>数据量趋势</span>
            </template>
            <div class="chart-container" ref="dataTrendChart" style="height: 300px"></div>
          </el-card>
        </el-col>
      </el-row>
    </el-dialog>

    <!-- 存储设置对话框 -->
    <el-dialog title="存储设置" v-model="settingsDialog.visible" width="600px" append-to-body>
      <el-form :model="storageSettings" label-width="120px">
        <el-form-item label="数据保留期">
          <el-input-number v-model="storageSettings.retentionDays" :min="1" :max="3650" placeholder="天" style="width: 200px" />
          <span style="margin-left: 10px; color: #666;">天</span>
        </el-form-item>
        <el-form-item label="自动清理">
          <el-switch v-model="storageSettings.autoCleanup" />
        </el-form-item>
        <el-form-item label="压缩存储">
          <el-switch v-model="storageSettings.compression" />
        </el-form-item>
        <el-form-item label="备份策略">
          <el-select v-model="storageSettings.backupStrategy" style="width: 200px">
            <el-option label="不备份" value="none" />
            <el-option label="每日备份" value="daily" />
            <el-option label="每周备份" value="weekly" />
            <el-option label="每月备份" value="monthly" />
          </el-select>
        </el-form-item>
        <el-form-item label="存储路径">
          <el-input v-model="storageSettings.storagePath" placeholder="请输入存储路径" />
        </el-form-item>
        <el-form-item label="最大存储大小">
          <el-input-number v-model="storageSettings.maxStorageSize" :min="1" :max="10240" placeholder="GB" style="width: 200px" />
          <span style="margin-left: 10px; color: #666;">GB</span>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="settingsDialog.visible = false">取 消</el-button>
          <el-button type="primary" @click="saveSettings">保 存</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 实时监控对话框 -->
    <el-dialog title="实时数据监控" v-model="realtimeDialog.visible" width="1000px" append-to-body>
      <div style="margin-bottom: 20px;">
        <el-button type="primary" @click="startRealtime" :disabled="realtimeRunning">开始监控</el-button>
        <el-button type="danger" @click="stopRealtime" :disabled="!realtimeRunning">停止监控</el-button>
        <el-button @click="clearRealtimeData">清空数据</el-button>
        <span style="margin-left: 20px;">监控状态：
          <el-tag :type="realtimeRunning ? 'success' : 'info'" size="small">
            {{ realtimeRunning ? '监控中' : '已停止' }}
          </el-tag>
        </span>
      </div>
      <div class="realtime-data-list">
        <div v-for="(item, index) in realtimeData" :key="index" class="realtime-item">
          <div class="realtime-header">
            <span class="realtime-time">{{ item.timestamp }}</span>
            <el-tag :type="getDataTypeTag(item.dataType)" size="small">{{ getDataTypeText(item.dataType) }}</el-tag>
            <span class="realtime-device">{{ item.deviceId }}</span>
          </div>
          <div class="realtime-content">{{ formatPayload(item.payload) }}</div>
        </div>
        <div v-if="realtimeData.length === 0" class="no-realtime-data">
          暂无实时数据
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="HistoryData" lang="ts">
import { ref, reactive, onMounted, nextTick } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Refresh, Download, PieChart, Setting, Delete, Monitor, View, Document, TrendCharts } from '@element-plus/icons-vue'
import { DataBoard, Clock, Cpu, Folder } from '@element-plus/icons-vue'

// 响应式数据
const loading = ref(false)
const total = ref(0)
const dataList = ref([])
const rawDataTab = ref('formatted')
const realtimeRunning = ref(false)
const realtimeData = ref([])

// 统计数据
const statistics = reactive({
  totalRecords: '2,458,632',
  todayRecords: '15,847',
  activeDevices: '1,256',
  storageSize: '125.6GB'
})

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  deviceId: '',
  dataType: '',
  topic: '',
  timeRange: []
})

// 对话框
const detailDialog = reactive({
  visible: false,
  data: null
})

const rawDataDialog = reactive({
  visible: false,
  data: ''
})

const statisticsDialog = reactive({
  visible: false
})

const settingsDialog = reactive({
  visible: false
})

const realtimeDialog = reactive({
  visible: false
})

// 存储设置
const storageSettings = reactive({
  retentionDays: 365,
  autoCleanup: true,
  compression: true,
  backupStrategy: 'weekly',
  storagePath: '/data/iot/history',
  maxStorageSize: 1024
})

// 计算属性
const formattedRawData = ref('')
const hexRawData = ref('')

// 获取数据类型标签样式
const getDataTypeTag = (type: string) => {
  switch (type) {
    case 'sensor': return 'primary'
    case 'status': return 'success'
    case 'alarm': return 'danger'
    case 'control': return 'warning'
    case 'log': return 'info'
    default: return 'info'
  }
}

// 获取数据类型文本
const getDataTypeText = (type: string) => {
  switch (type) {
    case 'sensor': return '传感器数据'
    case 'status': return '状态数据'
    case 'alarm': return '告警数据'
    case 'control': return '控制指令'
    case 'log': return '系统日志'
    default: return '未知'
  }
}

// 获取QoS标签样式
const getQosTag = (qos: string) => {
  switch (qos) {
    case '0': return 'info'
    case '1': return 'warning'
    case '2': return 'danger'
    default: return 'info'
  }
}

// 格式化载荷数据
const formatPayload = (payload: string) => {
  if (!payload) return ''
  try {
    const data = JSON.parse(payload)
    return JSON.stringify(data).substring(0, 100) + (JSON.stringify(data).length > 100 ? '...' : '')
  } catch {
    return payload.substring(0, 100) + (payload.length > 100 ? '...' : '')
  }
}

// 格式化数据大小
const formatDataSize = (size: number) => {
  if (size < 1024) return size + 'B'
  if (size < 1024 * 1024) return (size / 1024).toFixed(1) + 'KB'
  return (size / 1024 / 1024).toFixed(1) + 'MB'
}

// 查询历史数据列表
const getList = () => {
  loading.value = true
  // 模拟数据
  setTimeout(() => {
    dataList.value = [
      {
        id: 'DATA001',
        deviceId: 'DEVICE_001',
        deviceName: '温湿度传感器01',
        dataType: 'sensor',
        topic: '/device/sensor/temperature',
        payload: '{"temperature": 25.6, "humidity": 60.2, "timestamp": "2024-01-15T14:30:25Z"}',
        qos: '1',
        dataSize: 128,
        receiveTime: '2024-01-15 14:30:25',
        storageStatus: 'stored'
      },
      {
        id: 'DATA002',
        deviceId: 'DEVICE_002',
        deviceName: '智能门锁01',
        dataType: 'status',
        topic: '/device/lock/status',
        payload: '{"status": "locked", "battery": 85, "signal": -45, "timestamp": "2024-01-15T14:28:15Z"}',
        qos: '2',
        dataSize: 156,
        receiveTime: '2024-01-15 14:28:15',
        storageStatus: 'stored'
      },
      {
        id: 'DATA003',
        deviceId: 'DEVICE_003',
        deviceName: '烟雾报警器01',
        dataType: 'alarm',
        topic: '/device/alarm/smoke',
        payload: '{"alarm_type": "smoke_detected", "level": "high", "location": "kitchen", "timestamp": "2024-01-15T14:25:30Z"}',
        qos: '2',
        dataSize: 198,
        receiveTime: '2024-01-15 14:25:30',
        storageStatus: 'stored'
      },
      {
        id: 'DATA004',
        deviceId: 'DEVICE_004',
        deviceName: '智能开关01',
        dataType: 'control',
        topic: '/device/switch/control',
        payload: '{"command": "turn_on", "device_id": "DEVICE_004", "user_id": "USER_001", "timestamp": "2024-01-15T14:20:10Z"}',
        qos: '1',
        dataSize: 142,
        receiveTime: '2024-01-15 14:20:10',
        storageStatus: 'cached'
      },
      {
        id: 'DATA005',
        deviceId: 'SYSTEM_001',
        deviceName: '系统网关',
        dataType: 'log',
        topic: '/system/gateway/log',
        payload: '{"level": "info", "message": "Device DEVICE_001 connected", "source": "gateway", "timestamp": "2024-01-15T14:15:45Z"}',
        qos: '0',
        dataSize: 168,
        receiveTime: '2024-01-15 14:15:45',
        storageStatus: 'stored'
      }
    ]
    total.value = 2458632
    loading.value = false
  }, 1000)
}

// 搜索
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置
const resetQuery = () => {
  queryParams.deviceId = ''
  queryParams.dataType = ''
  queryParams.topic = ''
  queryParams.timeRange = []
  handleQuery()
}

// 查看设备详情
const viewDeviceDetail = (row: any) => {
  ElMessage.info('查看设备详情：' + row.deviceId)
}

// 查看载荷
const viewPayload = (row: any) => {
  viewDetail(row)
}

// 查看详情
const viewDetail = (row: any) => {
  detailDialog.data = row
  detailDialog.visible = true
}

// 查看原始数据
const viewRawData = (row: any) => {
  rawDataDialog.data = row.payload
  try {
    const parsed = JSON.parse(row.payload)
    formattedRawData.value = JSON.stringify(parsed, null, 2)
  } catch {
    formattedRawData.value = row.payload
  }
  
  // 转换为十六进制
  hexRawData.value = Array.from(new TextEncoder().encode(row.payload))
    .map(b => b.toString(16).padStart(2, '0'))
    .join(' ')
  
  rawDataDialog.visible = true
}

// 数据分析
const analyzeData = (row: any) => {
  ElMessage.info('数据分析功能开发中：' + row.deviceId)
}

// 导出单条记录
const exportSingle = (row: any) => {
  ElMessage.success('导出成功：' + row.id)
}

// 删除记录
const deleteRecord = (row: any) => {
  ElMessageBox.confirm('确认删除该条历史数据记录？', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
    getList()
  })
}

// 导出数据
const handleExport = () => {
  ElMessage.success('数据导出中，请稍候...')
}

// 刷新数据
const handleRefresh = () => {
  getList()
}

// 显示统计分析
const showStatistics = () => {
  statisticsDialog.visible = true
  // 这里可以初始化图表
  nextTick(() => {
    // 初始化图表的代码
  })
}

// 显示设置
const showSettings = () => {
  settingsDialog.visible = true
}

// 数据清理
const handleCleanup = () => {
  ElMessageBox.confirm('确认清理过期的历史数据？此操作不可恢复！', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('数据清理完成')
  })
}

// 显示实时监控
const showRealtime = () => {
  realtimeDialog.visible = true
}

// 复制原始数据
const copyRawData = () => {
  navigator.clipboard.writeText(rawDataDialog.data).then(() => {
    ElMessage.success('数据已复制到剪贴板')
  })
}

// 保存设置
const saveSettings = () => {
  ElMessage.success('存储设置保存成功')
  settingsDialog.visible = false
}

// 开始实时监控
const startRealtime = () => {
  realtimeRunning.value = true
  ElMessage.success('开始实时监控')
  
  // 模拟实时数据
  const realtimeInterval = setInterval(() => {
    if (!realtimeRunning.value) {
      clearInterval(realtimeInterval)
      return
    }
    
    const mockData = {
      timestamp: new Date().toLocaleString(),
      deviceId: 'DEVICE_' + String(Math.floor(Math.random() * 10) + 1).padStart(3, '0'),
      dataType: ['sensor', 'status', 'alarm', 'control'][Math.floor(Math.random() * 4)],
      payload: JSON.stringify({
        value: Math.floor(Math.random() * 100),
        status: Math.random() > 0.5 ? 'online' : 'offline',
        timestamp: new Date().toISOString()
      })
    }
    
    realtimeData.value.unshift(mockData)
    
    // 限制数据数量
    if (realtimeData.value.length > 100) {
      realtimeData.value = realtimeData.value.slice(0, 100)
    }
  }, 2000)
}

// 停止实时监控
const stopRealtime = () => {
  realtimeRunning.value = false
  ElMessage.info('已停止实时监控')
}

// 清空实时数据
const clearRealtimeData = () => {
  realtimeData.value = []
  ElMessage.success('实时数据已清空')
}

// 组件挂载
onMounted(() => {
  getList()
})
</script>

<style scoped>
.mb8 {
  margin-bottom: 8px;
}

.stat-card {
  cursor: pointer;
  transition: all 0.3s;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.stat-item {
  display: flex;
  align-items: center;
  padding: 10px;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  margin-right: 15px;
}

.stat-content {
  flex: 1;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
  line-height: 1;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin-top: 5px;
}

.payload-content {
  cursor: pointer;
  color: #409EFF;
  font-family: 'Courier New', monospace;
  font-size: 12px;
}

.payload-content:hover {
  text-decoration: underline;
}

.payload-detail {
  background: #f5f7fa;
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  padding: 15px;
  max-height: 300px;
  overflow-y: auto;
}

.payload-detail pre {
  margin: 0;
  font-family: 'Courier New', monospace;
  font-size: 13px;
  line-height: 1.5;
  color: #303133;
}

.raw-data-container {
  height: 400px;
}

.code-editor {
  background: #2d3748;
  color: #e2e8f0;
  border-radius: 4px;
  padding: 15px;
  height: 350px;
  overflow-y: auto;
  font-family: 'Courier New', monospace;
  font-size: 13px;
  line-height: 1.5;
}

.code-editor pre {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-all;
}

.chart-container {
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #909399;
  background: #f5f7fa;
  border-radius: 4px;
}

.realtime-data-list {
  max-height: 500px;
  overflow-y: auto;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 10px;
}

.realtime-item {
  margin-bottom: 15px;
  padding: 12px;
  background: #f5f7fa;
  border-radius: 6px;
  border-left: 4px solid #409EFF;
}

.realtime-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}

.realtime-time {
  font-size: 12px;
  color: #666;
  font-weight: 500;
}

.realtime-device {
  font-size: 12px;
  color: #409EFF;
  font-weight: 500;
}

.realtime-content {
  font-family: 'Courier New', monospace;
  font-size: 13px;
  background: #fff;
  padding: 8px;
  border-radius: 4px;
  border: 1px solid #e4e7ed;
  word-break: break-all;
}

.no-realtime-data {
  text-align: center;
  color: #999;
  padding: 40px;
  font-size: 14px;
}
</style>